var dragging = false; //TODO: temporary solution! not nice
var seniors = {
    "desktop" : {
        createIcons : function () {
            id = albireo.desktop.getElement();
            width = 360;
            height = 350;
            albireo.desktop.icons = new albireo.icons(id,width,height);
            alert("a");
            $.getJSON(
                getapps,
                function (data) {
                    albireo.desktop.icons.setCols(data.length);
                    for (var i in data) {
                        app = data[i];
                        albireo.desktop.icons.set(new albireo.icon("images/seniors/"+app.name+".png",app.title,app.url
                            ),i);
                    }
                    albireo.desktop.icons.draw();
                    id.css({
                        left:-id.width()/2
                    });
                    id.draggable({
                        'axis' : 'x',           
                        'distance' : 30,
                        start : function (event, ui) {
                            dragging = true;
                        },
                        stop : function () {
                            setTimeout(function() {
                                dragging = false;
                            }, 200);
                            $(id).dragBack({
                                "parent" : $(window), 
                                "widthType" :"children"
                            });
                        }
                    });
                }
                );
        }
    },
    "taskbar" : {
        "draw" : function () {
            albireo.taskbar.start.draw();
        }
    },
    "start" : {
        "draw" : function () {
            id = albireo.taskbar.start.getElement();
            id.html(
                albireo.taskbar.start.getImage() ? "<div id='taskbarstartimage'><img src='"+albireo.taskbar.start.getImage()+"' width='191' height='137' alt='start image'/></div>" : ""
                ).mousedown(function () {
                $(".dialogApplication").dialog("minimize");
                $(".albireoicon").fadeIn("slow");
                $('#startbutton').fadeOut("slow");
            });
        }
    },
    "applications" : {
        createSingle : function (name, windowed) {
            for (var i in albireo.applications.processes) {
                if (albireo.applications.processes[i].getName() == name) {
                    albireo.applications.processes[i].toggle();
                    return null;
                }
            }
            return albireo.applications.create(name, windowed);
        },
        createWindow : function (name,content,options) {
            if (options == undefined) {
                options = {};
            }
            var process = albireo.applications.createSingle(name,true);
            if (process != null) {
                var app = $("<div id='"+albireo.applications.APPLICATION+process.getId()+"' class='dialogApplication'>"+content+"</div>").appendTo($("#desktop"))
                .dialog(
                {
                    minimizable: true,
                    maximized: true,
                    //TODO: ADD users before close to handle close button (for saving and so on) event listener
                    focus: process.focus,
                    close: process.close,
                    resizable: false,
                    draggable: false
                });
                $(app).find('.resizable').width($(app).width()).height($(app).height());

                $('.ui-dialog-titlebar-maxi').click(function() {
                    $('#'+albireo.applications.APPLICATION+process.getId()).focus();
                });
                albireo.applications.focused = process;
            }
        }
    }
};
albireo.icons = function (element,width,height) {
    this.width = width;
    this.height = height;
    this.element = element;
    this.cols = 0;
    this.icons = new Array();

    this.getWidth = function () {
        return this.width;
    };
    this.getHeight = function () {
        return this.height;
    };

    this.getCols = function () {
        return this.cols;
    };
    this.setCols = function (cols) {
        this.cols = cols;
    };

    this.draw = function () {
        helper = this;
        this.element.find(".albireoicon").remove();
        for ( i = 0; i < this.getCols();i++) {
            if (this.get(i) != null) {
                this.get(i).element = $('<div class="albireoicon"></div>').appendTo(this.element).css(
                {
                    top: Math.max(albireo.desktop.getHeight()/2 - this.getHeight()/2 - 50,0),
                    left: i*this.getWidth(),
                    width: this.getWidth(),
                    height: this.getHeight()
                })
                .attr("id", this.get(i).url)
                .html('<img src="'+this.get(i).image+'" class="iconimage" /><div class="iconName">'+this.get(i).name+'</div>')
                .bind('click', function () {
                    if (!dragging) { 
                        albireo.applications.createFromUrl($(this).find(".iconName").html(),$(this).attr("id"));
                        $(".albireoicon").fadeOut("fast");
                        $('#startbutton').fadeIn("slow");
                    }
                });
            }
        }
        
        $(".iconimage").css({
            'maxHeight' : this.height,
            'maxWidth': this.width
        });
    };
    
    this.resize = function () {
        this.element.find(".albireoicon").css({
            top: Math.max(albireo.desktop.getHeight()/2 - this.getHeight()/2 - 50,0)
            });
    };

    this.set = function (icon,place) {
        this.icons[place] = icon;
    };
    this.get = function (place) {
        return this.icons[place];
    };
};
$(document).unbind('ready', initfnc);

initfnc = function () {
    if (typeof document.body.onselectstart!="undefined")
        document.body.onselectstart=function(){
            return false
        }
    else if (typeof document.body.style.MozUserSelect!="undefined")
        document.body.style.MozUserSelect="none";

    var url = "images/seniors/wall.jpg";
    albireo.taskbar.setElement($("#taskbar"));
    //    albireo.taskbar.startmenu.setElement($("#albireostartmenu"));
    albireo.taskbar.start.setElement($("#startbutton"));
    albireo.taskbar.start.setImage("systemApps/shared/start_button.png");
    albireo.taskbar.draw();
    //$("#taskbarmini").hide();
    //    albireo.taskbar.init();

    albireo.desktop.setElement($("#desktop"));
    albireo.desktop.setColor("#000");
    albireo.desktop.display(url);
    albireo.desktop.createIcons();
    albireo.keys.init();
    $(window).resize( function () {
        albireo.desktop.display(url);
        id = albireo.desktop.getElement();
        id.width($(window).width()).height($(window).height());
        albireo.desktop.icons.resize();
        $(".dialogApplication iframe").css({
            "height" : $(window).height(), 
            "width" : $(window).width()
            });
        $(".dialogApplication").dialog("maximize");
    });
    $(window).load( function () {
        $("#loading").remove();
    });
}

$(document).ready(initfnc);
$.extend(albireo.desktop, seniors.desktop);
$.extend(albireo.taskbar, seniors.taskbar);
$.extend(albireo.taskbar.start, seniors.start);
$.extend(albireo.applications, seniors.applications);
